﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
	public class ChuyenGoldDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion		

		#region Methods

		/// <summary>
		/// Saves a record to the ChuyenGold table.
		/// </summary>
		public virtual int Insert(ChuyenGoldInfo chuyenGoldInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				
				new SqlParameter("@CodeMember_From", chuyenGoldInfo.CodeMember_From),
                new SqlParameter("@CodeMember_To", chuyenGoldInfo.CodeMember_To),
				new SqlParameter("@IP", chuyenGoldInfo.IP),
				new SqlParameter("@CreateDate", chuyenGoldInfo.CreateDate),
				new SqlParameter("@Gold", chuyenGoldInfo.Gold),
				new SqlParameter("@Description", chuyenGoldInfo.Description),
				new SqlParameter("@NumberAcount", chuyenGoldInfo.NumberAcount),
				new SqlParameter("@NameBank", chuyenGoldInfo.NameBank),
				new SqlParameter("@CodeActive", chuyenGoldInfo.CodeActive),
				new SqlParameter("@IsActive", chuyenGoldInfo.IsActive),
				new SqlParameter("@Status", chuyenGoldInfo.Status),
				new SqlParameter("@FullName_To", chuyenGoldInfo.FullName_To),
                new SqlParameter("@NameAcount", chuyenGoldInfo.NameAcount),
                new SqlParameter("@Type", chuyenGoldInfo.Type)

			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the ChuyenGold table.
		/// </summary>
		public virtual void Update(ChuyenGoldInfo chuyenGoldInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", chuyenGoldInfo.Id),
				new SqlParameter("@CodeMember_From", chuyenGoldInfo.CodeMember_From),
               	new SqlParameter("@CodeMember_To", chuyenGoldInfo.CodeMember_To),
				new SqlParameter("@IP", chuyenGoldInfo.IP),
				new SqlParameter("@CreateDate", chuyenGoldInfo.CreateDate),
				new SqlParameter("@Gold", chuyenGoldInfo.Gold),
				new SqlParameter("@Description", chuyenGoldInfo.Description),
				new SqlParameter("@NumberAcount", chuyenGoldInfo.NumberAcount),
				new SqlParameter("@NameBank", chuyenGoldInfo.NameBank),
				new SqlParameter("@CodeActive", chuyenGoldInfo.CodeActive),
				new SqlParameter("@IsActive", chuyenGoldInfo.IsActive),
				new SqlParameter("@Status", chuyenGoldInfo.Status),
				new SqlParameter("@FullName_To", chuyenGoldInfo.FullName_To),
                new SqlParameter("@NameAcount", chuyenGoldInfo.NameAcount),
                new SqlParameter("@Type", chuyenGoldInfo.Type)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_Update", parameters);
		}




        public DataSet ListChuyenGoldPage(int Id, DateTime createOnFrom, DateTime createOnTo, int currPage, int recodperPage, int pageSize, int total)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                          {
                                              new SqlParameter("@Id", Id),
                                              new SqlParameter("@CreatedOnFrom",createOnFrom),
                                              new SqlParameter("@CreatedOnTo",createOnTo),
                                              new SqlParameter("@currPage", currPage),
                                              new SqlParameter("@recodperPage", recodperPage),
                                              new SqlParameter("@pageSize", pageSize), 
                                              new SqlParameter("@total", total)
                                          };
            using (DataSet ds = SqlClientUtility.ExecuteDataSet(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_search_Page", parameters))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    return ds;
                return null;
            }
        }

        public int GetCount(int Id, DateTime createOnFrom, DateTime createOnTo)
        {
            SqlParameter[] parameters = new SqlParameter[]
                                            {
                                                new SqlParameter("@Id",Id), 
                                                new SqlParameter("@CreateOnFrom",createOnFrom),
                                                new SqlParameter("@CreateOnTo",createOnTo)                                               
                                            };
            return
                (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_GetCount", parameters);

        }

		/// <summary>
		/// Deletes a record from the ChuyenGold table by its primary key.
		/// </summary>
		public virtual void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the ChuyenGold table.
		/// </summary>
		public virtual ChuyenGoldInfo Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeChuyenGoldInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the ChuyenGold table.
		/// </summary>
		public CHRTList<ChuyenGoldInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_SelectAll"))
			{
				CHRTList<ChuyenGoldInfo> chuyenGoldInfoList = new CHRTList<ChuyenGoldInfo>();
				while (dataReader.Read())
				{
					ChuyenGoldInfo chuyenGoldInfo = MakeChuyenGoldInfo(dataReader);
					chuyenGoldInfoList.Add(chuyenGoldInfo);
				}

				return chuyenGoldInfoList;
			}
		}
        #region Danh sach chuyen gold qua tài khoản ví Dealcom + ATM
        public virtual int SelectCount(int status,int type, string CodeMember_To, string CodeMember_Form, string StartDate, string EndDate)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{           
                new SqlParameter("@Status", status),
                new SqlParameter("@Type", type),
				new SqlParameter("@CodeMember_To", CodeMember_To),
                new SqlParameter("@CodeMember_Form", CodeMember_Form),
                new SqlParameter("@StarDatetime", StartDate),
                new SqlParameter("@EndDatetime", EndDate)
			};

            return (int)SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_Search", parameters);
        }

        public virtual DataTable Search(int status, int type,string CodeMember_To, string CodeMember_Form, string starDatetime, string endDatetime, int beginRow, int endRow, string orderByColumn, bool isTotalRow)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                 new SqlParameter("@Status", status),
                new SqlParameter("@Type", type),
               	new SqlParameter("@CodeMember_To", CodeMember_To),
                new SqlParameter("@CodeMember_Form", CodeMember_Form),
                new SqlParameter("@StarDatetime", starDatetime),
                new SqlParameter("@EndDatetime", endDatetime),               
                new SqlParameter("@BeginRow", beginRow),
                new SqlParameter("@EndRow", endRow),
                new SqlParameter("@OrderByColumn", orderByColumn),
                new SqlParameter("@IsTotalRow", false.ToString())
			};

            return SqlClientUtility.ExecuteDataTable(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_Search", parameters);
        }

        #endregion

      

        #region cập nhật lại trạng thái
        public virtual void UpdateStatus(int id, int status)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id),
                new SqlParameter("@Status", status),
			};

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ChuyenGold_UpdateStatus", parameters);
        }

        #endregion
        /// <summary>
		/// Creates a new instance of the ChuyenGold class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual ChuyenGoldInfo MakeChuyenGoldInfo(SqlDataReader dataReader)
		{
			ChuyenGoldInfo chuyenGoldInfo = new ChuyenGoldInfo();
			chuyenGoldInfo.Id = SqlClientUtility.GetInt32(dataReader,DbConstants.CHUYENGOLD.ID, 0);
			chuyenGoldInfo.CodeMember_To = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.CODEMEMBER_TO, String.Empty);
			chuyenGoldInfo.CodeMember_From = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.CODEMEMBER_FROM, String.Empty);
			chuyenGoldInfo.IP = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.IP, String.Empty);
			chuyenGoldInfo.CreateDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.CHUYENGOLD.CREATEDATE, DateTime.Now);
			chuyenGoldInfo.Gold = SqlClientUtility.GetInt32(dataReader,DbConstants.CHUYENGOLD.GOLD, 0);
			chuyenGoldInfo.Description = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.DESCRIPTION, String.Empty);
			chuyenGoldInfo.NumberAcount = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.NUMBERACOUNT, String.Empty);
			chuyenGoldInfo.NameBank = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.NAMEBANK, String.Empty);
			chuyenGoldInfo.CodeActive = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.CODEACTIVE, String.Empty);
			chuyenGoldInfo.IsActive = SqlClientUtility.GetBoolean(dataReader,DbConstants.CHUYENGOLD.ISACTIVE, false);
			chuyenGoldInfo.Status = SqlClientUtility.GetInt32(dataReader,DbConstants.CHUYENGOLD.STATUS, 0);
			chuyenGoldInfo.FullName_To = SqlClientUtility.GetString(dataReader,DbConstants.CHUYENGOLD.FULLNAME_TO, String.Empty);
            chuyenGoldInfo.NameAcount = SqlClientUtility.GetString(dataReader, DbConstants.CHUYENGOLD.NAMEACOUNT, String.Empty);
            chuyenGoldInfo.Type = SqlClientUtility.GetInt32(dataReader, DbConstants.CHUYENGOLD.TYPE, 0);
			return chuyenGoldInfo;
		}

		#endregion
	}
}
